/**
 * This file is part of the dspace-poltman project http://www.poltman.com.
 *
 * This is free software; you can redistribute it and/or modify it under
 * the terms of DuraSpace DSpace license detailed in the LICENSE and NOTICE 
 * files at the root of the source tree and available online at
 * http://www.dspace.org/license/
 * 
 */
package com.poltman.dscentral.dscentral.menuPolicies;

import java.util.List;

import org.apache.log4j.Logger;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.util.GenericForwardComposer;
import org.zkoss.zkplus.spring.SpringUtil;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;

import com.poltman.dscentral.db.entity.pm.MainMenuGroupsEntity;
import com.poltman.dscentral.db.service.pm.MainMenuGroupsService;

/**
 * 
 * @author Zbigniew Ciok; z.ciok@poltman.com
 * @version $Revision: 1670 $
 *
 */
@SuppressWarnings("rawtypes")
public class ExistingGroupsController extends GenericForwardComposer {


	private static final long serialVersionUID = 604561205049336079L;
	
	@SuppressWarnings("unused")
	private static Logger log = Logger.getLogger(ExistingGroupsController.class);	
	
	
	private Combobox groupsCbx;
	private Textbox existingGroupDescrTextbox;
	private Listbox existingGroupsListbox;
	
	private String selectedShortcut = "";
	

	@SuppressWarnings("unchecked")
	@Override
    public void doAfterCompose(org.zkoss.zk.ui.Component comp) throws Exception {
    	super.doAfterCompose(comp);
    }	
	
	/**
	 * Select group
	 * @throws Exception
	 */
	public void onSelect$groupsCbx() throws Exception {
		
		selectedShortcut = groupsCbx.getSelectedItem().getValue();
		MainMenuGroupsService mainMenuGroupsService = (MainMenuGroupsService) SpringUtil.getBean("mainMenuGroupsService");
		List<MainMenuGroupsEntity> groups = mainMenuGroupsService.findByShortcut(selectedShortcut);
		existingGroupDescrTextbox.setValue(groups.get(0).getDescr());	
		
		List<Component> items = this.existingGroupsListbox.getChildren();
		items.clear();				
		
		ExistingGroupsVM vm = new ExistingGroupsVM();
		vm.setSelectedShortcut(selectedShortcut);
		ListModelList model =  vm.getExistingGroupsItems();
		this.existingGroupsListbox.setModel(model);			
	}
	
	/**
	 * Refresh shortcut list
	 * @throws Exception
	 */
	public void onClick$refreshExistingBtn() throws Exception {
		ExistingGroupsVM model = new ExistingGroupsVM();
		this.groupsCbx.setModel(model.getExistingGroupsShortcuts());	
		
		this.existingGroupDescrTextbox.setValue(null);
		
		List<Component> items = this.existingGroupsListbox.getChildren();
		items.clear();		
	}
	
	/**
	 * Delete group
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public void onClick$deleteSelectedBtn() throws Exception {
		log.info("onClick$deleteSelectedBtn(); selectdShortcut = " + this.selectedShortcut);
		
		if ((this.selectedShortcut == null) || (this.selectedShortcut.isEmpty())) {
			//TODO: i18n
			Messagebox.show("Nothing to delete...", "DSCentral Error", 0,  Messagebox.ERROR);
			return;			
		} else {
			//TODO: i18n			
			Messagebox.show("You are about to delete " + this.selectedShortcut + ".", "DSCentral", Messagebox.OK | Messagebox.CANCEL, Messagebox.QUESTION, new org.zkoss.zk.ui.event.EventListener() {
			    public void onEvent(Event evt) throws InterruptedException, Exception {
			    	if (evt.getName().equals(Messagebox.ON_OK)) {
						MainMenuGroupsService mainMenuGroupsService = (MainMenuGroupsService) SpringUtil.getBean("mainMenuGroupsService");
						mainMenuGroupsService.deleteGroupByShortcut(selectedShortcut);
						onClick$refreshExistingBtn();
			    	} 
			    }
			});
		}
	}	
}
